package com.gdsdxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gdsdxy.dto.ResultVo;
import com.gdsdxy.entity.Comments;
import com.gdsdxy.entity.CommentsLike;
import com.gdsdxy.mapper.CommentsLikeMapper;
import com.gdsdxy.mapper.CommentsMapper;
import com.gdsdxy.service.ICommentsLikeService;
import com.gdsdxy.service.ICommentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CommentsLikeServiceImpl extends ServiceImpl<CommentsLikeMapper,CommentsLike> implements ICommentsLikeService {
    @Autowired
    ICommentsService iCommentsService;
    @Override
    public ResultVo like(Long cid, Long uid) {
        //查询是否有记录，如果有记录直接返回
        CommentsLike commentsLike=getOne(new QueryWrapper<CommentsLike>().lambda()
                .eq(CommentsLike::getCid,cid)
                .eq(CommentsLike::getUid,uid));
        if(commentsLike!=null){
            return ResultVo.fail("点赞失败，你已经点过了");
        }
        //先对点赞表添加记录 ， 然后 评论表 对应的评论 点赞数加1
        CommentsLike commentsLike1=new CommentsLike();
        commentsLike1.setCid(cid);
        commentsLike1.setUid(uid);
        boolean save = save(commentsLike1);
        iCommentsService.update(new UpdateWrapper<Comments>().lambda()
                .setSql("likes=likes+1")
                .eq(Comments::getId,cid));
        return save ? ResultVo.ok("点赞成功") : ResultVo.fail("点赞失败");
    }
}
